package org.wanda.pv.service.hy.impl;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.business.ProjectCheckState;
import org.wanda.pv.model.hy.HyProjectInfo;
import org.wanda.pv.model.hy.HyUserInfo;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.business.ProjectCheckNoticeService;
import org.wanda.pv.service.hy.HyDrawingService;
import org.wanda.pv.service.hy.HyProjectInfoService;
import org.wanda.pv.util.HyUtil;

/**
 * @author lxy
 * Created by 2018/12/25 11:43
 */
@Service
public class HyProjectInfoServiceImpl  extends BaseServiceImpl<HyProjectInfo> implements HyProjectInfoService{
	public HyProjectInfoServiceImpl(Dao dao) {
		super(dao);
	}

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ProjectCheckNoticeService projectCheckNoticeService;
	@Autowired
	private HyDrawingService hyDrawingService;
	@Override
	public Object checkProject(String projectId, Long hyUserId,String access_token , String projectFlowChildrenNodeId) {
		// 查询出项目的审图状态来，然后根据状态去判断用户能不能进行这个操作。
		ProjectCheckState projectCheckState = dao().fetch(ProjectCheckState.class, Cnd.where("hy_project_id","=",projectId));
		HyUserInfo hyUserInfo = dao().fetch(HyUserInfo.class,hyUserId);
		dao().fetchLinks(hyUserInfo,null);
		//判断这个项目有没进入初审，没有就插入
		if (projectCheckState != null){
			// 判断项目的审核进度 0：初审中状态 1：会审状态 2：复审中状态 3 复审结束
			switch (projectCheckState.getProjectCheckSchedule()){
				case 0 :
					//项目的审核状态  0：设计确认 1：三审确认 2：项目确认
					if ("三审单位".equals(hyUserInfo.getHyRole().getRoleType())){
						//如果当前状态是0，说明项目已经设计确认过了，所以开始三审确认
						projectCheckState.setFirstPvUserId(hyUserId);
						// 判断如果项目公司先点了
						if (projectCheckState.getFirstWdUserId() != null || "".equals(projectCheckState.getFirstWdUserId())) {
							projectCheckState.setProjectCheckState(-1);
							projectCheckState.setProjectCheckSchedule(1);
							HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.FIRSTCHECKOVERPROJECT);
							projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.FIRSTCHECKOVERPROJECT,1);
							logger.info(HyUtil.FIRSTCHECKOVERPROJECT);
						} else {
							projectCheckState.setProjectCheckState(1);
						}
						dao().update(projectCheckState);
					} else if ("项目公司".equals(hyUserInfo.getHyRole().getRoleType())){
						if (projectCheckState.getFirstPvUserId() != null || "".equals(projectCheckState.getFirstPvUserId())) {
							projectCheckState.setProjectCheckState(-1);
							projectCheckState.setProjectCheckSchedule(1);
							HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.FIRSTCHECKOVERPROJECT);
							projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.FIRSTCHECKOVERPROJECT,1);
							logger.info(HyUtil.FIRSTCHECKOVERPROJECT);
						} else {
							projectCheckState.setProjectCheckState(2);
						}
						projectCheckState.setFirstWdUserId(hyUserId);
						dao().update(projectCheckState);
					}
					break;
				// 会审状态
				case 1 :
					if ("设计总包".equals(hyUserInfo.getHyRole().getRoleType())){
						projectCheckState.setRepetitionDeUserId(hyUserId);
						// 设置项目进度为复审中状态
						projectCheckState.setProjectCheckSchedule(2);
						//设置为 状态为设计确认状态
						projectCheckState.setProjectCheckState(0);
						dao().update(projectCheckState);
						HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.REPETIONCHECKINGPROJECT);
						projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.REPETIONCHECKINGPROJECT,2);
						logger.info(HyUtil.REPETIONCHECKINGPROJECT);
					} else {
						return Result.error("项目用户角色不符合","项目用户角色不符合");
					}

					break;
				case 2 :

						if ("三审单位".equals(hyUserInfo.getHyRole().getRoleType())){
							//如果当前状态是0，说明项目已经设计确认过了，所以开始三审确认
							projectCheckState.setRepetitionPvUserId(hyUserId);
							// 判断如果项目公司先点了
							if (projectCheckState.getRepetitionWdUserId() != null || "".equals(projectCheckState.getRepetitionWdUserId())) {
								projectCheckState.setProjectCheckState(1);
								projectCheckState.setProjectCheckSchedule(3);
								HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.REPETIONCHECKOVERPROJECT);
								projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.REPETIONCHECKOVERPROJECT,3);
								logger.info(HyUtil.REPETIONCHECKOVERPROJECT);
								hyDrawingService.sync2dDraw(projectFlowChildrenNodeId,access_token);
							} else {
								projectCheckState.setProjectCheckState(1);
							}
							dao().update(projectCheckState);

						} else if ("项目公司".equals(hyUserInfo.getHyRole().getRoleType())){
							if (projectCheckState.getRepetitionPvUserId() != null || "".equals(projectCheckState.getRepetitionPvUserId())) {
								projectCheckState.setProjectCheckState(2);
								projectCheckState.setProjectCheckSchedule(3);
								HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.REPETIONCHECKOVERPROJECT);
								projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.REPETIONCHECKOVERPROJECT,3);
								logger.info(HyUtil.REPETIONCHECKOVERPROJECT);
								hyDrawingService.sync2dDraw(projectFlowChildrenNodeId,access_token);
							} else {
								projectCheckState.setProjectCheckState(2);
							}
							projectCheckState.setRepetitionWdUserId(hyUserId);
							dao().update(projectCheckState);
						}
					break;
				default:
					break;
			}
		} else {

			if ("设计总包".equals(hyUserInfo.getHyRole().getRoleType())){
				projectCheckState = startCheckProject(projectId,hyUserId);
				HyUtil.sendPlanMessage(access_token,projectFlowChildrenNodeId,"",HyUtil.FIRSTCHECKINGPROJECT);
				projectCheckNoticeService.addProjectCheckNotice(projectId,hyUserId,projectCheckState.getId(),HyUtil.FIRSTCHECKINGPROJECT,0);
				logger.info(HyUtil.FIRSTCHECKINGPROJECT);
			} else {
				return Result.error("项目用户角色不符合","项目用户角色不符合");
			}
		}
		projectCheckState = dao().fetch(ProjectCheckState.class,Cnd.where("hy_project_id","=",projectId));
		return Result.success("success",projectCheckState);
	}


	/**
	 * 开始初审
	 * @param projectId
	 * @param hyUserId
	 * @return
	 */
	public ProjectCheckState startCheckProject(String projectId,Long hyUserId) {
		ProjectCheckState projectCheckState = new ProjectCheckState();
		projectCheckState.setHyProjectId(projectId);
		// 设置项目进度为初审进度
		projectCheckState.setProjectCheckSchedule(0);
		//设置为 状态为设计确认状态
		projectCheckState.setProjectCheckState(0);
		projectCheckState.setFirstDeUserId(hyUserId);
		projectCheckState = dao().insert(projectCheckState);

		return projectCheckState;
	}
}
